package com.westcom.vulb.activity;

import com.westcom.vulb.logic.SessionManager;
import com.westcom.vulb.logic.share;
import com.westcom.vulb.protocal.http.Response;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class InputAddShareMemberMailActivity extends BasicActivity 
{
	private EditText ed_mail;
	private TextView tv_done;
	private ImageView iv_back;
	private OnClickListener l = null;
	private int mCurrentSheetId = -1;
	private ProgressDialog mLoadingDialog;
	private SessionManager sm;
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_input_add_share_member_mail);
		mCurrentSheetId = this.getIntent().getIntExtra("sheetId", -1);
		init();
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	private void init()
	{
		sm = SessionManager.getInstance();
		initView();
	}
	private void initView()
	{
		l = new OnClickListener()
		{

			@Override
			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
				switch(v.getId())
				{
					case R.id.iv_input_share_mail_left:
					{
						finish();
					}break;
					case R.id.tv_input_share_mail_right:
					{
						String mail = ed_mail.getText().toString();
						if(mail.equals(""))
						{
							launchToast(R.string.email_empty_error);
							return;
						}
						mLoadingDialog = ProgressDialog.show(InputAddShareMemberMailActivity.this,getResources().getString(R.string.wait),getResources().getString(R.string.processing));
						InviteUserByMailTask inviteUserByMailTask = new InviteUserByMailTask();
						inviteUserByMailTask.execute(new String[] {mail});
					}break;
				}
			}};
		iv_back = (ImageView)findViewById(R.id.iv_input_share_mail_left);
		tv_done = (TextView)findViewById(R.id.tv_input_share_mail_right);
		ed_mail = (EditText)findViewById(R.id.et_input_share_mail);
		iv_back.setOnClickListener(l);
		tv_done.setOnClickListener(l);
	}
	private String InviteUserByMail(String[] mails)
	{
		Response res = null;
    	share sh = share.getInstanse();
    	res = sh.InviteWithMail(sm.getCurrSessionId(), mCurrentSheetId, mails);
    	if(res.errno == Response.SUCCESS)
    	{
    		return null;
    	}else
    	{
    		return res.errmsg;
    	}
	}
	private class InviteUserByMailTask extends AsyncTask<String, Integer, String> {
        @Override
        protected String doInBackground(String... params) 
        {
            return InviteUserByMail(params);
        }
        @Override
        protected void onCancelled() {
            super.onCancelled();
            if(mLoadingDialog.isShowing())
        	{
        		mLoadingDialog.hide();
        	}
        }
        @Override  
        protected void onPostExecute(String result) 
        {  
        	super.onPostExecute(result);
        	if(result != null)
        	{
        		launchToast(result);
        		ed_mail.requestFocus();
        	}else
        	{
        		ed_mail.setText("");
        	}
        	if(mLoadingDialog.isShowing())
        	{
        		mLoadingDialog.hide();
        	}
        }
	}
}
